package com.jwaoo.mobile.view.forgotpassword.presenter;

import android.app.Activity;

import com.dx.as.common.CommonUtils;
import com.dx.as.common.log.LogUtil;
import com.dx.as.listener.IProInterface;
import com.dx.as.listener.IViewInterface;
import com.jwaoo.mobile.forchat.IFroChatMouleImpl;
import com.jwaoo.mobile.view.forgotpassword.IPasswordView;
import com.jwaoo.mobile.view.forgotpassword.model.PasswordModel;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by jwaoo on 2017/10/18.
 */

public class PasswordPresenter extends IProInterface{

    private PasswordModel forgotModel;
    private int forgotCode;
    private IPasswordView iView;
    public PasswordPresenter(Activity activity, IViewInterface iViewInterface) {
        super(activity, iViewInterface);
        forgotModel = new PasswordModel(getContext(),this);
        iView = (IPasswordView) iViewInterface;
    }

    @Override
    public void initialataData() {

    }

    public void doSendEmail(String email){
        Map<String,Object> map = new HashMap<>();
        map.put("account",email);
        forgotCode = forgotModel.doSendEmail(map);
    }

    public void doUpdatePassword(String orgPassword , String newPassword){
        Map<String,Object> map = new HashMap<>();
        map.put("orgPassword", orgPassword);
        map.put("newPassword", newPassword);
        forgotModel.doUpdatePassword(map);
    }

    @Override
    public void onResponse(Object response, int flg) {
        if (flg != forgotCode){
            Map<String,Object> map = (Map<String,Object>)response;
            if (map.containsKey("token")){
                    String token = map.get("token").toString();
                    CommonUtils.saveToken(token);
                    int perfect = (int) Double.parseDouble(map.get("perfect").toString());
                    CommonUtils.savePerfect(perfect);
                    LogUtil.d("LoginActivity", CommonUtils.getPushToken());
                    //完善资料
                    if (perfect == 0){
                        iView.onNotPerfect();
                    }
                IFroChatMouleImpl.getInstance().stopChatSystem();
                IFroChatMouleImpl.getInstance().loginChatSystem();
            }
        }
        iView.onHttpRequestComplete(true);
    }

    @Override
    public void onFailure(int code, String errorMsg) {
        super.onFailure(code, errorMsg);
        iView.onHttpRequestComplete(false);
    }

    @Override
    public void onFailure(Throwable t) {
        super.onFailure(t);
        iView.onHttpRequestComplete(false);
    }
}
